package pl.pw.mkmw.pnganimator.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import pl.pw.mkmw.pnganimator.image.SImage;

/**
 *
 * @author Michał
 */
public class ImagePanel extends javax.swing.JPanel {

    private static final Logger LOG = Logger.getLogger(ImagePanel.class.getName());

    /**
     * Creates new form ImagePanel
     */
    public ImagePanel() {
        initComponents();
    }
    private ArrayList<SImage> images;
    public int indexToShow = -1;

    public void setImages(ArrayList<SImage> a) {
        this.images = a;
//        jButton1.setEnabled(true);
//        jButton2.setEnabled(true);
        indexToShow = 0;
    }

    public void update(Graphics g) {
        LOG.log(Level.INFO, "Called update");
        paint(g);
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);
        if (indexToShow != -1) {
            BufferedImage image;
            SImage im = images.get(indexToShow);
            image = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_RGB);

            Color[][] c = im.getColor();

            for (int x = 0; x < image.getWidth(); x++) {
                for (int y = 0; y < image.getHeight(); y++) {
                    image.setRGB(x, y, c[y][x].getRGB());

                }
            }
            image = scaleImage(image, 800, 600, Color.darkGray);
            this.setSize(image.getWidth(), image.getHeight());

            g.drawImage(image, 0, 0, this); // see javadoc for more info on the parameters            
        }

    }

    public BufferedImage scaleImage(BufferedImage img, int width, int height,
            Color background) {
        int imgWidth = img.getWidth();
        int imgHeight = img.getHeight();
        if (imgWidth * height < imgHeight * width) {
            width = imgWidth * height / imgHeight;
        } else {
            height = imgHeight * width / imgWidth;
        }
        BufferedImage newImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = newImage.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.setBackground(background);
            g.clearRect(0, 0, width, height);
            g.drawImage(img, 0, 0, width, height, null);
        } finally {
            g.dispose();
        }
        return newImage;
    }

    private class Repainter extends Thread {

        public void run() {
            for (indexToShow = 0; indexToShow < images.size(); indexToShow++) {

                repaint();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void play(JDialog jd) {
        Repainter r = new Repainter();
        r.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
